﻿#region Đào Mạnh Hòa
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.Transactions;
using Common;

namespace BusinessLogic
{
    public static class HDBanBuon
    {
        static SqlCommand cmd;
        static ArrayList arr;
        static SqlDataReader dr;
        static SqlConnection cn;

        #region select
        public static Entities.HDBanBuon[] Select()
        {
            Entities.HDBanBuon[] list = null;
            try
            {
                string sql = Common.Constants.HDBanBuon.SelectHDBanBuon;
                cn = DataAccess.Connection.OpenConnection();
                cmd = new SqlCommand(sql, cn);
                cmd.CommandType = CommandType.StoredProcedure;
                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.HDBanBuon hdbanbuon = new Entities.HDBanBuon();
                    hdbanbuon.HDBanBuonID = int.Parse(dr[Common.Constants.HDBanBuon.HDBanBuonID].ToString());
                    hdbanbuon.MaHDBanBuon = dr[Common.Constants.HDBanBuon.MaHDBanBuon].ToString();
                    hdbanbuon.NgayLap = DateTime.Parse(dr[Common.Constants.HDBanBuon.NgayLap].ToString());
                    hdbanbuon.MaKhachHang = dr[Common.Constants.HDBanBuon.MaKhachHang].ToString();
                    hdbanbuon.TenKhachHang = dr[Common.Constants.HDBanBuon.TenKhachHang].ToString();
                    hdbanbuon.NoHienThoi = dr[Common.Constants.HDBanBuon.NoHienThoi].ToString();
                    hdbanbuon.NguoiNhanHang = dr[Common.Constants.HDBanBuon.NguoiNhanHang].ToString();
                    hdbanbuon.MaDonDatHang = dr[Common.Constants.HDBanBuon.MaDonDatHang].ToString();
                    hdbanbuon.MaKho = dr[Common.Constants.HDBanBuon.MaKho].ToString();
                    hdbanbuon.TenKho = dr[Common.Constants.HDBanBuon.TenKho].ToString();
                    hdbanbuon.HanThanhToan = DateTime.Parse(dr[Common.Constants.HDBanBuon.HanThanhToan].ToString());
                    hdbanbuon.MaNhanVien = dr[Common.Constants.HDBanBuon.MaNhanVien].ToString();
                    hdbanbuon.TenNhanVien = dr[Common.Constants.HDBanBuon.TenNhanVien].ToString();
                    hdbanbuon.PhanTramChietKhauThuongmai = int.Parse(dr[Common.Constants.HDBanBuon.PhanTramChietKhauThuongMai].ToString());
                    hdbanbuon.ChietKhau = dr[Common.Constants.HDBanBuon.ChietKhau].ToString();
                    hdbanbuon.ThanhToanNgay = dr[Common.Constants.HDBanBuon.ThanhToanNgay].ToString();
                    hdbanbuon.ThanhToanSauKhiLapPhieu = dr[Common.Constants.HDBanBuon.ThanhToanSauKhiLapPhieu].ToString();
                    hdbanbuon.ThueGTGT = dr[Common.Constants.HDBanBuon.ThueGTGT].ToString();
                    hdbanbuon.TongTienThanhToan = Common.Utilities.FormatMoney(dr[Common.Constants.HDBanBuon.TongTienThanhToan].ToString());
                    hdbanbuon.MaTheVip = dr[Common.Constants.HDBanBuon.MaTheVip].ToString();
                    hdbanbuon.GiaTriThe = dr[Common.Constants.HDBanBuon.GiaTriThe].ToString();
                    hdbanbuon.GhiChu = dr[Common.Constants.HDBanBuon.GhiChu].ToString();
                    hdbanbuon.Deleted = bool.Parse(dr[Common.Constants.HDBanBuon.Deleted].ToString());
                    arr.Add(hdbanbuon);
                }
                int n = arr.Count;
                list = (Entities.HDBanBuon[])arr.ToArray(typeof(Entities.HDBanBuon));
                return list;
            }
            catch
            {
                return new Entities.HDBanBuon[0];
            }
            finally
            {
                cmd.Connection.Dispose();
                cn.Close();
            }
        }

        public static Entities.HDBanBuon[] Select(Entities.HDBanBuon hdbanbuon1)
        {
            Entities.HDBanBuon[] list = null;
            try
            {
                string sql = Common.Constants.HDBanBuon.SelectHDBanBuonTheoMa;
                cn = DataAccess.Connection.OpenConnection();
                cmd = new SqlCommand(sql, cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(Common.Constants.HDBanBuon.MaHDBanBuon, SqlDbType.VarChar, 50).Value = hdbanbuon1.MaHDBanBuon;
                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.HDBanBuon hdbanbuon = new Entities.HDBanBuon();
                    hdbanbuon.HDBanBuonID = int.Parse(dr[Common.Constants.HDBanBuon.HDBanBuonID].ToString());
                    hdbanbuon.MaHDBanBuon = dr[Common.Constants.HDBanBuon.MaHDBanBuon].ToString();
                    hdbanbuon.NgayLap = DateTime.Parse(dr[Common.Constants.HDBanBuon.NgayLap].ToString());
                    hdbanbuon.MaKhachHang = dr[Common.Constants.HDBanBuon.MaKhachHang].ToString();
                    hdbanbuon.TenKhachHang = dr[Common.Constants.HDBanBuon.TenKhachHang].ToString();
                    hdbanbuon.NoHienThoi = dr[Common.Constants.HDBanBuon.NoHienThoi].ToString();
                    hdbanbuon.NguoiNhanHang = dr[Common.Constants.HDBanBuon.NguoiNhanHang].ToString();
                    hdbanbuon.MaDonDatHang = dr[Common.Constants.HDBanBuon.MaDonDatHang].ToString();
                    hdbanbuon.MaKho = dr[Common.Constants.HDBanBuon.MaKho].ToString();
                    hdbanbuon.TenKho = dr[Common.Constants.HDBanBuon.TenKho].ToString();
                    hdbanbuon.HanThanhToan = DateTime.Parse(dr[Common.Constants.HDBanBuon.HanThanhToan].ToString());
                    hdbanbuon.MaNhanVien = dr[Common.Constants.HDBanBuon.MaNhanVien].ToString();
                    hdbanbuon.TenNhanVien = dr[Common.Constants.HDBanBuon.TenNhanVien].ToString();
                    hdbanbuon.PhanTramChietKhauThuongmai = int.Parse(dr[Common.Constants.HDBanBuon.PhanTramChietKhauThuongMai].ToString());
                    hdbanbuon.ChietKhau = dr[Common.Constants.HDBanBuon.ChietKhau].ToString();
                    hdbanbuon.ThanhToanNgay = dr[Common.Constants.HDBanBuon.ThanhToanNgay].ToString();
                    hdbanbuon.ThanhToanSauKhiLapPhieu = dr[Common.Constants.HDBanBuon.ThanhToanSauKhiLapPhieu].ToString();
                    hdbanbuon.ThueGTGT = dr[Common.Constants.HDBanBuon.ThueGTGT].ToString();
                    hdbanbuon.TongTienThanhToan = Common.Utilities.FormatMoney(dr[Common.Constants.HDBanBuon.TongTienThanhToan].ToString());
                    hdbanbuon.MaTheVip = dr[Common.Constants.HDBanBuon.MaTheVip].ToString();
                    hdbanbuon.GiaTriThe = dr[Common.Constants.HDBanBuon.GiaTriThe].ToString();
                    hdbanbuon.GhiChu = dr[Common.Constants.HDBanBuon.GhiChu].ToString();
                    hdbanbuon.Deleted = bool.Parse(dr[Common.Constants.HDBanBuon.Deleted].ToString());
                    arr.Add(hdbanbuon);
                }
                int n = arr.Count;
                list = (Entities.HDBanBuon[])arr.ToArray(typeof(Entities.HDBanBuon));
                return list;
            }
            catch
            {
                return new Entities.HDBanBuon[0];
            }
            finally
            {
                cmd.Connection.Dispose();
                cn.Close();
            }
        }

        public static Entities.HDBanBuon[] SelectTheoMaKH(string makh)
        {
            Entities.HDBanBuon[] list = null;
            try
            {
                string sql = Common.Constants.HDBanBuon.SelectHDBanBuonTheoMaKH;
                cn = DataAccess.Connection.OpenConnection();
                cmd = new SqlCommand(sql, cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(Common.Constants.HDBanBuon.MaKhachHang, SqlDbType.VarChar, 50).Value = makh;
                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.HDBanBuon hdbanbuon = new Entities.HDBanBuon();
                    hdbanbuon.HDBanBuonID = int.Parse(dr[Common.Constants.HDBanBuon.HDBanBuonID].ToString());
                    hdbanbuon.MaHDBanBuon = dr[Common.Constants.HDBanBuon.MaHDBanBuon].ToString();
                    hdbanbuon.NgayLap = DateTime.Parse(dr[Common.Constants.HDBanBuon.NgayLap].ToString());
                    hdbanbuon.MaKhachHang = dr[Common.Constants.HDBanBuon.MaKhachHang].ToString();
                    hdbanbuon.TenKhachHang = dr[Common.Constants.HDBanBuon.TenKhachHang].ToString();
                    hdbanbuon.NoHienThoi = dr[Common.Constants.HDBanBuon.NoHienThoi].ToString();
                    hdbanbuon.NguoiNhanHang = dr[Common.Constants.HDBanBuon.NguoiNhanHang].ToString();
                    hdbanbuon.MaDonDatHang = dr[Common.Constants.HDBanBuon.MaDonDatHang].ToString();
                    hdbanbuon.MaKho = dr[Common.Constants.HDBanBuon.MaKho].ToString();
                    hdbanbuon.TenKho = dr[Common.Constants.HDBanBuon.TenKho].ToString();
                    hdbanbuon.HanThanhToan = DateTime.Parse(dr[Common.Constants.HDBanBuon.HanThanhToan].ToString());
                    hdbanbuon.MaNhanVien = dr[Common.Constants.HDBanBuon.MaNhanVien].ToString();
                    hdbanbuon.TenNhanVien = dr[Common.Constants.HDBanBuon.TenNhanVien].ToString();
                    hdbanbuon.PhanTramChietKhauThuongmai = int.Parse(dr[Common.Constants.HDBanBuon.PhanTramChietKhauThuongMai].ToString());
                    hdbanbuon.ChietKhau = dr[Common.Constants.HDBanBuon.ChietKhau].ToString();
                    hdbanbuon.ThanhToanNgay = dr[Common.Constants.HDBanBuon.ThanhToanNgay].ToString();
                    hdbanbuon.ThanhToanSauKhiLapPhieu = dr[Common.Constants.HDBanBuon.ThanhToanSauKhiLapPhieu].ToString();
                    hdbanbuon.ThueGTGT = dr[Common.Constants.HDBanBuon.ThueGTGT].ToString();
                    hdbanbuon.TongTienThanhToan = dr[Common.Constants.HDBanBuon.TongTienThanhToan].ToString();
                    hdbanbuon.MaTheVip = dr[Common.Constants.HDBanBuon.MaTheVip].ToString();
                    hdbanbuon.GiaTriThe = dr[Common.Constants.HDBanBuon.GiaTriThe].ToString();
                    hdbanbuon.GhiChu = dr[Common.Constants.HDBanBuon.GhiChu].ToString();
                    hdbanbuon.Deleted = bool.Parse(dr[Common.Constants.HDBanBuon.Deleted].ToString());
                    arr.Add(hdbanbuon);
                }
                int n = arr.Count;
                list = (Entities.HDBanBuon[])arr.ToArray(typeof(Entities.HDBanBuon));
                return list;
            }
            catch (Exception ex)
            {
                return new Entities.HDBanBuon[0];
            }
            finally
            {
                cmd.Connection.Dispose();
                cn.Close();
            }
        }

        public static Entities.HDBanBuon[] SelectTheoMaHDBanBuon(string maHDBanBuon)
        {
            Entities.HDBanBuon[] list = null;
            try
            {
                string sql = Common.Constants.HDBanBuon.SelectHDBanBuonTheoMaHDBanBuon;
                cn = DataAccess.Connection.OpenConnection();
                cmd = new SqlCommand(sql, cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(Common.Constants.HDBanBuon.MaHDBanBuon, SqlDbType.VarChar, 50).Value = maHDBanBuon;
                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.HDBanBuon hdbanbuon = new Entities.HDBanBuon();
                    hdbanbuon.HDBanBuonID = int.Parse(dr[Common.Constants.HDBanBuon.HDBanBuonID].ToString());
                    hdbanbuon.MaHDBanBuon = dr[Common.Constants.HDBanBuon.MaHDBanBuon].ToString();
                    hdbanbuon.NgayLap = DateTime.Parse(dr[Common.Constants.HDBanBuon.NgayLap].ToString());
                    hdbanbuon.MaKhachHang = dr[Common.Constants.HDBanBuon.MaKhachHang].ToString();
                    hdbanbuon.TenKhachHang = dr[Common.Constants.HDBanBuon.TenKhachHang].ToString();
                    hdbanbuon.NoHienThoi = dr[Common.Constants.HDBanBuon.NoHienThoi].ToString();
                    hdbanbuon.NguoiNhanHang = dr[Common.Constants.HDBanBuon.NguoiNhanHang].ToString();
                    hdbanbuon.MaDonDatHang = dr[Common.Constants.HDBanBuon.MaDonDatHang].ToString();
                    hdbanbuon.MaKho = dr[Common.Constants.HDBanBuon.MaKho].ToString();
                    hdbanbuon.TenKho = dr[Common.Constants.HDBanBuon.TenKho].ToString();
                    hdbanbuon.HanThanhToan = DateTime.Parse(dr[Common.Constants.HDBanBuon.HanThanhToan].ToString());
                    hdbanbuon.MaNhanVien = dr[Common.Constants.HDBanBuon.MaNhanVien].ToString();
                    hdbanbuon.TenNhanVien = dr[Common.Constants.HDBanBuon.TenNhanVien].ToString();
                    hdbanbuon.PhanTramChietKhauThuongmai = int.Parse(dr[Common.Constants.HDBanBuon.PhanTramChietKhauThuongMai].ToString());
                    hdbanbuon.ChietKhau = dr[Common.Constants.HDBanBuon.ChietKhau].ToString();
                    hdbanbuon.ThanhToanNgay = dr[Common.Constants.HDBanBuon.ThanhToanNgay].ToString();
                    hdbanbuon.ThanhToanSauKhiLapPhieu = dr[Common.Constants.HDBanBuon.ThanhToanSauKhiLapPhieu].ToString();
                    hdbanbuon.ThueGTGT = dr[Common.Constants.HDBanBuon.ThueGTGT].ToString();
                    hdbanbuon.TongTienThanhToan = dr[Common.Constants.HDBanBuon.TongTienThanhToan].ToString();
                    hdbanbuon.MaTheVip = dr[Common.Constants.HDBanBuon.MaTheVip].ToString();
                    hdbanbuon.GiaTriThe = dr[Common.Constants.HDBanBuon.GiaTriThe].ToString();
                    hdbanbuon.GhiChu = dr[Common.Constants.HDBanBuon.GhiChu].ToString();
                    hdbanbuon.Deleted = bool.Parse(dr[Common.Constants.HDBanBuon.Deleted].ToString());
                    arr.Add(hdbanbuon);
                }
                list = (Entities.HDBanBuon[])arr.ToArray(typeof(Entities.HDBanBuon));
                return list;
            }
            catch (Exception ex)
            {
                return new Entities.HDBanBuon[0];
            }
            finally
            {
                cmd.Connection.Dispose();
                cn.Close();
            }
        }
        #endregion

        #region insert
        // insert update
        public static int InsertUpdate(Entities.HDBanBuon hdbanbuon)
        {
            TransactionScope ts = null;
            int rt = 0;
            try
            {
                using (ts = new TransactionScope(TransactionScopeOption.Required))
                {
                    string sql = Common.Constants.HDBanBuon.InsertUpdateHDBanBuon;
                    cn = DataAccess.Connection.OpenConnection();
                    SqlCommand cmd = new SqlCommand(sql, cn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(Common.Constants.HDBanBuon.HanhDong, SqlDbType.NVarChar, 50).Value = hdbanbuon.HanhDong;
                    cmd.Parameters.Add(Common.Constants.HDBanBuon.HDBanBuonID, SqlDbType.Int).Value = hdbanbuon.HDBanBuonID;
                    cmd.Parameters.Add(Common.Constants.HDBanBuon.MaHDBanBuon, SqlDbType.VarChar, 50).Value = hdbanbuon.MaHDBanBuon;
                    cmd.Parameters.Add(Common.Constants.HDBanBuon.NgayLap, SqlDbType.DateTime).Value = hdbanbuon.NgayLap;
                    cmd.Parameters.Add(Common.Constants.HDBanBuon.NguoiNhanHang, SqlDbType.NVarChar, 200).Value = hdbanbuon.NguoiNhanHang;
                    cmd.Parameters.Add(Common.Constants.HDBanBuon.HanThanhToan, SqlDbType.DateTime).Value = hdbanbuon.HanThanhToan;
                    cmd.Parameters.Add(Common.Constants.HDBanBuon.MaDonDatHang, SqlDbType.VarChar, 50).Value = hdbanbuon.MaDonDatHang;
                    cmd.Parameters.Add(Common.Constants.HDBanBuon.MaKhachHang, SqlDbType.VarChar, 50).Value = hdbanbuon.MaKhachHang;
                    cmd.Parameters.Add(Common.Constants.HDBanBuon.TenKhachHang, SqlDbType.NVarChar, 200).Value = hdbanbuon.TenKhachHang;
                    cmd.Parameters.Add(Common.Constants.HDBanBuon.NoHienThoi, SqlDbType.Float).Value = hdbanbuon.NoHienThoi;
                    cmd.Parameters.Add(Common.Constants.HDBanBuon.MaKho, SqlDbType.VarChar, 50).Value = hdbanbuon.MaKho;
                    cmd.Parameters.Add(Common.Constants.HDBanBuon.TenKho, SqlDbType.NVarChar, 200).Value = hdbanbuon.TenKho;
                    cmd.Parameters.Add(Common.Constants.HDBanBuon.MaNhanVien, SqlDbType.VarChar, 50).Value = hdbanbuon.MaNhanVien;
                    cmd.Parameters.Add(Common.Constants.HDBanBuon.TenNhanVien, SqlDbType.NVarChar, 200).Value = hdbanbuon.TenNhanVien;
                    cmd.Parameters.Add(Common.Constants.HDBanBuon.PhanTramChietKhauThuongMai, SqlDbType.Float).Value = hdbanbuon.PhanTramChietKhauThuongmai;
                    cmd.Parameters.Add(Common.Constants.HDBanBuon.ChietKhau, SqlDbType.Float).Value = hdbanbuon.ChietKhau;
                    cmd.Parameters.Add(Common.Constants.HDBanBuon.ThueGTGT, SqlDbType.VarChar, 50).Value = hdbanbuon.ThueGTGT;
                    cmd.Parameters.Add(Common.Constants.HDBanBuon.ThanhToanNgay, SqlDbType.Float).Value = hdbanbuon.ThanhToanNgay;
                    cmd.Parameters.Add(Common.Constants.HDBanBuon.ThanhToanSauKhiLapPhieu, SqlDbType.Float).Value = hdbanbuon.ThanhToanSauKhiLapPhieu;
                    cmd.Parameters.Add(Common.Constants.HDBanBuon.TongTienThanhToan, SqlDbType.Float).Value = hdbanbuon.TongTienThanhToan;
                    cmd.Parameters.Add(Common.Constants.HDBanBuon.MaTheVip, SqlDbType.VarChar, 50).Value = hdbanbuon.MaTheVip;
                    cmd.Parameters.Add(Common.Constants.HDBanBuon.GiaTriThe, SqlDbType.Float).Value = hdbanbuon.GiaTriThe;
                    cmd.Parameters.Add(Common.Constants.HDBanBuon.GhiChu, SqlDbType.NVarChar, 200).Value = hdbanbuon.GhiChu;
                    cmd.Parameters.Add(Common.Constants.HDBanBuon.Deleted, SqlDbType.Bit).Value = hdbanbuon.Deleted;
                    rt = cmd.ExecuteNonQuery();
                    cn.Close();
                    // insert chi tiết
                    if (BusinessLogic.ChiTietHDBanBuon.InsertUpdate(hdbanbuon.ChiTietHDBanBuon) == 0)
                    {
                        ts.Dispose();
                        return 0;
                    }
                    // trừ số lượng
                    if (TruSoLuong(hdbanbuon.ChiTietKhoHang) == 0)
                    {
                        ts.Dispose();
                        return 0;
                    }
                    // update dư nợ
                    if (hdbanbuon.KhachHang != null)
                    {
                        Entities.ChiTietCongNoKH temp = new Entities.ChiTietCongNoKH(
                            "1",
                            hdbanbuon.KhachHang.MaKhachHang,
                            hdbanbuon.MaKho,
                            hdbanbuon.KhachHang.DuNo,
                            false);
                        Entities.ChiTietCongNoKH[] chitiet = BusinessLogic.ChiTietCongNoKH.Select(temp);
                        if (chitiet != null && chitiet.Length != 0)
                            temp.HanhDong = "Update";
                        else
                            temp.HanhDong = "Insert";
                        if (BusinessLogic.ChiTietCongNoKH.InsertUpdate(temp) == 0)
                        {
                            ts.Dispose();
                            return 0;
                        }
                    }
                    // check update thanh toán ngay hóa đơn nhập
                    if (double.Parse(hdbanbuon.ThanhToanNgay) != 0)
                    {
                        // Check insert phiếu thanh toán nhà cung cấp
                        if (InsertUpdate(hdbanbuon.PhieuTTKH)==0)
                        {
                            ts.Dispose();
                            return 0;
                        }
                        // check insert chi tiết phiếu thanh toán nhà cung cấp
                        if (InsertUpdate(hdbanbuon.ChiTietPhieuTTKH)==0)
                        {
                            ts.Dispose();
                            return 0;
                        }
                    }
                    if (hdbanbuon.TheVip != null)
                    {
                        if (BusinessLogic.TheVip.InsertUpdate(hdbanbuon.TheVip) == 0)
                        {
                            ts.Dispose();
                            return 0;
                        }
                    }
                    if (hdbanbuon.DiemThuongKhachHang != null)
                    {
                        if (BusinessLogic.DiemThuongKhachHang.InsertUpdate(hdbanbuon.DiemThuongKhachHang) == 0)
                        {
                            ts.Dispose();
                            return 0;
                        }
                    }
                    if (hdbanbuon.DonDatHangKH != null)
                    {
                        if (UpdateTrangThaiDDHKH(hdbanbuon.DonDatHangKH) == 0)
                        {
                            ts.Dispose();
                            return 0;
                        }
                    }
                    LuuVietHeThong.LuuViet(
                        "Thêm hóa đơn",
                        hdbanbuon.TaiKhoan.MaNhanVien,
                        "Thêm hóa đơn mới " + hdbanbuon.MaHDBanBuon,
                        hdbanbuon.TaiKhoan.TenDangNhap,
                        DateTime.Now.ToString("dd/MM/YYYY"));
                    ts.Complete();
                    return rt;
                }
            }
            catch
            {
                ts.Dispose();
                return 0;
            }

        }
        #region thêm phiếu thanh toán của khách hàng
        public static int InsertUpdate(Entities.PhieuTTCuaKH phieuthanhtoankhachhang)
        {
            try
            {
                string sql = Common.Constants.PhieuTTCuaKH.InsertUpdatePhieuTTKH;
                cn = DataAccess.Connection.OpenConnection();
                cmd = new SqlCommand(sql, cn);

                cmd.Parameters.Add(Common.Constants.PhieuTTCuaKH.HanhDong, SqlDbType.NVarChar, 20).Value = phieuthanhtoankhachhang.HanhDong;
                cmd.Parameters.Add(Common.Constants.PhieuTTCuaKH.PhieuTTCuaKHID, SqlDbType.Int).Value = phieuthanhtoankhachhang.PhieuTTCuaKHID;
                cmd.Parameters.Add(Common.Constants.PhieuTTCuaKH.MaPhieuTTCuaKH, SqlDbType.VarChar, 50).Value = phieuthanhtoankhachhang.MaPhieuTTCuaKH;
                cmd.Parameters.Add(Common.Constants.PhieuTTCuaKH.NgayLap, SqlDbType.DateTime).Value = phieuthanhtoankhachhang.NgayLap;
                cmd.Parameters.Add(Common.Constants.PhieuTTCuaKH.MaKhachHang, SqlDbType.VarChar, 50).Value = phieuthanhtoankhachhang.MaKhachHang;
                cmd.Parameters.Add(Common.Constants.PhieuTTCuaKH.TenKhachHang, SqlDbType.NVarChar, 200).Value = phieuthanhtoankhachhang.TenKhachHang;
                cmd.Parameters.Add(Common.Constants.PhieuTTCuaKH.MaNhanVien, SqlDbType.NVarChar, 50).Value = phieuthanhtoankhachhang.MaNhanVien;
                cmd.Parameters.Add(Common.Constants.PhieuTTCuaKH.MaKho, SqlDbType.NVarChar, 50).Value = phieuthanhtoankhachhang.MaKho;
                cmd.Parameters.Add(Common.Constants.PhieuTTCuaKH.TongThanhToan, SqlDbType.Float).Value = phieuthanhtoankhachhang.TongThanhToan;
                cmd.Parameters.Add(Common.Constants.PhieuTTCuaKH.NoHienThoi, SqlDbType.Float).Value = phieuthanhtoankhachhang.NoHienThoi;
                cmd.Parameters.Add(Common.Constants.PhieuTTCuaKH.NguoiNop, SqlDbType.NVarChar, 200).Value = phieuthanhtoankhachhang.NguoiNop;
                cmd.Parameters.Add(Common.Constants.PhieuTTCuaKH.LoaiPhieu, SqlDbType.Bit).Value = phieuthanhtoankhachhang.LoaiPhieu;
                cmd.Parameters.Add(Common.Constants.PhieuTTCuaKH.GhiChu, SqlDbType.NVarChar, 200).Value = phieuthanhtoankhachhang.GhiChu;
                cmd.Parameters.Add(Common.Constants.PhieuTTCuaKH.Deleted, SqlDbType.Bit).Value = phieuthanhtoankhachhang.Deleted;
                int i = cmd.ExecuteNonQuery();
                cmd.Connection.Dispose();
                cn.Close();
                cn = null;
                return i;
            }
            catch
            {
                return 0;
            }
        }
        #endregion

        #region thêm chi tiết phiếu thanh toán của khách hàng
        public static int InsertUpdate(Entities.ChiTietPhieuTTCuaKH chitietphieuttkh)
        {
            try
            {
                string sql = Common.Constants.ChiTietPhieuTTKH.InsertUpdate;
                cn = DataAccess.Connection.OpenConnection();
                cmd = new SqlCommand(sql, cn);

                cmd.Parameters.Add(Common.Constants.ChiTietPhieuTTKH.HanhDong, SqlDbType.NVarChar, 50).Value = chitietphieuttkh.HanhDong;
                cmd.Parameters.Add(Common.Constants.ChiTietPhieuTTKH.MaHoaDon, SqlDbType.VarChar, 50).Value = chitietphieuttkh.MaHDBanHang;
                cmd.Parameters.Add(Common.Constants.ChiTietPhieuTTKH.MaPhieuTTCuaKH, SqlDbType.VarChar, 50).Value = chitietphieuttkh.MaPhieuTTCuaKH;
                cmd.Parameters.Add(Common.Constants.ChiTietPhieuTTKH.TongTien, SqlDbType.Float).Value = chitietphieuttkh.TongTienThanhToan;
                cmd.Parameters.Add(Common.Constants.ChiTietPhieuTTKH.TienNo, SqlDbType.Float).Value = chitietphieuttkh.SoNo;
                cmd.Parameters.Add(Common.Constants.ChiTietPhieuTTKH.ThanhToan, SqlDbType.Float).Value = chitietphieuttkh.ThanhToan;
                cmd.Parameters.Add(Common.Constants.ChiTietPhieuTTKH.GhiChu, SqlDbType.NVarChar, 200).Value = "";
                cmd.Parameters.Add(Common.Constants.ChiTietPhieuTTKH.Deleted, SqlDbType.Bit).Value = false;
                int i = cmd.ExecuteNonQuery();
                cmd.Connection.Dispose();
                cn.Close();
                cn = null;
                return i;
            }
            catch
            {
                return 0;
            }
        }

        public static int InsertUpdate(Entities.ChiTietPhieuTTCuaKH[] chitietphieuttkh)
        {
            try
            {
                for (int i = 0; i < chitietphieuttkh.Length; i++)
                {
                    if (InsertUpdate(chitietphieuttkh[i]) == 0)
                        return 0;
                }
                return 1;
            }
            catch
            {
                return 0;
            }

        }
        #endregion

        #region Cập nhật trạng thái ĐĐHKH
        public static int UpdateTrangThaiDDHKH(Entities.DonDatHangKH hdbanbuon)
        {
            int rt = 0;
            try
            {
                string sql = Common.Constants.HDBanBuon.UpdateTrangThai;
                 cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(sql, cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(Common.Constants.DonDatHangKH.MaDonDatHangKH, SqlDbType.VarChar, 50).Value = hdbanbuon.MaDonDatHangKH;
                rt = cmd.ExecuteNonQuery();
                cn.Close();
                return rt;
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); return 0; }

        }
        #endregion

        #region Trừ Số Lượng
        public static int TruSoLuong(Entities.ChiTietKhoHang chititetkhohang)
        {
            try
            {
                string sql = Common.Constants.TraLaiNCC.TruSoLuong;
                cn = DataAccess.Connection.OpenConnection();
                cmd = new SqlCommand(sql, cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(Common.Constants.ChiTietKhoHang.MaKho, SqlDbType.VarChar, 50).Value = chititetkhohang.MaKho;
                cmd.Parameters.Add(Common.Constants.ChiTietKhoHang.MaHangHoa, SqlDbType.VarChar, 50).Value = chititetkhohang.MaHangHoa;
                cmd.Parameters.Add(Common.Constants.ChiTietKhoHang.SoLuong, SqlDbType.Float).Value = chititetkhohang.SoLuong;

                int i = cmd.ExecuteNonQuery();

                cmd.Connection.Dispose();
                cn.Close();
                cn = null;
                return i;
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                return 0;
            }
        }

        public static int TruSoLuong(Entities.ChiTietKhoHang[] chitietkhohang)
        {
            try
            {
                foreach (Entities.ChiTietKhoHang item in chitietkhohang)
                {
                    if (TruSoLuong(item) == 0)
                    {
                        return 0;
                    }
                }
                return 1;
            }
            catch
            {
                return 0;
            }
        }
        #endregion
        #endregion
    }
}
#endregion